<?php

class Exts_Controller_User extends Exts_Controller_Action {

	public function loginAction() {
		$this->view->headScript()->appendFile('/js/js/login.box.js');
		$this->view->headLink()->appendStylesheet('/js/css/login.box.css');
		$this->view->headLink()->appendStylesheet('/css/login.css');
		$this->_helper->layout->setLayout('login');
		$this->view->menuHtml = "";
		
		if( $this->_config->menu->show == "off" ){
			$this->_session->menu = "";
		}else{
			if (isset($this->_session->menu)) {
				$this->_session->menu = "";
			}
		}
		$form = new Application_Form_Login();
		if ($this->getRequest()->isPost()) {
			
			if ($form->isValid($this->_getAllParams())) {
				$user = new Application_Model_Users();
				
				

				if ($user->login($form->getValue('username'), $form->getValue('password'))) {
					$row = $user->exists($form->getValue('username'));
					
					$u = new stdClass();
					$u->username = $row->username;
					$u->id = $row->user_id;

					$rulesModel = new Exts_Models_Rules();
					$u->role = $user->getRole($u->id);

					$rules = $rulesModel->getRulesByUser($u->id);
					$acl = new Zend_Acl();
					$acl->addRole(new Zend_Acl_Role($u->role));

					$resources = array();
					foreach ($rules as $rule) {
						if (!in_array($rule['name'], $resources)) {
							$resources[] = $rule['name'];
							$acl->add(new Zend_Acl_Resource($rule['name']));
						}
						$acl->allow($u->role, $rule['name'], $rule['operation']);
					}
					$u->resources = $resources;
					$u->acl = $acl;
					$u->auth = Zend_Auth::getInstance();
					$this->_session->user = $u;
					

					if( $this->_config->menu->show == "off" ){
						$this->_session->menu = "";
					}else{
						/**
						 * Traemos el menu. A partir de los permisos, y el usuario
						 */
						$menu = $this->view->menu()->get('admin');
						$m = '';
						foreach ($menu as $item) {
							$m .= '<li>'
								. '<a href="' . $this->view->url(array('controller' => $item['controller'], 'action' => $item['action']))
								. '">' . $item['title'] . '</a>'
								. '</li>';
						}
						/**
						 * Grabamos todo en session
						 */
						$this->_session->menu = $m;
					}

					return $this->_redirect('/');
				}
				$this->_helper->getHelper('message')->addError('Check your username or Password.');
			}
			$this->_helper->getHelper('message')->addError('Check data is empty or invalid.');
		}
		$this->view->form = $form;
	}

	public function logoutAction() {
		$this->_session->user->auth->clearIdentity();
		$this->_session->user = null;
		return $this->_redirect('/user/login/');
	}

	public function registryAction() {
		$form = new Application_Form_Registry();
		$this->_helper->layout->setLayout('login');

		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->_getAllParams())) {

				if ($form->getValue('username') != $form->getValue('username_2')) {
					$this->_helper->getHelper('message')->addError('The email not the same');
					$this->view->form = $form;
					return $this->render();
				}

				if ($form->getValue('password') != $form->getValue('password_2')) {
					$this->_helper->getHelper('message')->addError('The password not the same');
					$this->view->form = $form;
					return $this->render();
				}

				$user = new Application_Model_Users();
				if ($user->exist($form->getValue('username'))) {
					$this->_helper->getHelper('message')->addError('The username exist. Use other.');
					$this->view->form = $form;
					return $this->render();
				}
				try {
					$userId = $user->save($form->getValues());
				} catch (Exception $e) {
					$this->_helper->getHelper('message')->addError('An occurred error try again later.');
					$this->view->form = $form;
					return $this->render();
				}

				$this->_helper->getHelper('message')->addSuccess('Registry successfull.');
				$user = new Application_Model_Users();
				if ($user->login($form->getValue('username'), $form->getValue('password'))) {
					$row = $user->exist($form->getValue('username'));
					$u = new stdClass();
					$u->username = $row->username;
					$u->id = $row->user_id;
					$this->_session->user = $u;
					return $this->_redirect('/');
				}
			} else {
				$this->_helper->getHelper('message')->addError('The formulary content error.');
			}
		}
		$this->view->form = $form;
	}

}

